package com.yunhui.controller;

import com.aliyuncs.exceptions.ClientException;
import com.yunhui.entity.Merchant;
import com.yunhui.service.impl.MerchantServiceImpl;
import com.yunhui.util.PageSupport;
import com.yunhui.util.SmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import org.springframework.util.StringUtils;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  商家的前端控制层
 * </p>
 *
 * @author WGH
 * @since 2019-12-09
 */
@Controller
@RequestMapping("/merchant")
public class MerchantController {

    @Autowired
    private MerchantServiceImpl impl;

    @RequestMapping("/Login")
    public String login(){
        return "Login";
    }

    /**
     * 验证码
     * @param phone
     * @param yzmla
     * @return
     */

    @RequestMapping("yzm")
    @ResponseBody
    public Object yzm(String phone, Integer yzmla){
        System.err.println("phone:  "+phone+"\nyzmla:  "+yzmla);
        SmsUtil smsUtil = new SmsUtil();
        try {
            smsUtil.sendSms(phone,yzmla);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 注册
     * @param account
     * @param phone
     * @param eamil
     * @param status
     * @param password
     * @param password2
     * @return
     */
    @RequestMapping(value = {"/doregist"})
    public String addUser(@RequestParam("account") String account,
                          @RequestParam("phone") String phone,
                          @RequestParam("eamil") String eamil,
                          /*@RequestParam("createDate") Date createDate,*/
                          @RequestParam("merchantname") String merchantname,
                          @RequestParam("status") Integer status,
                          @RequestParam("password") String password,
                          @RequestParam("password2") String password2,
                          @RequestParam("imgUrl") MultipartFile file) {

        Integer statu = 0;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String stringdate = formatter.format(new Date()); //获取系统当前时间

        System.err.println("stringdate : " + stringdate);

        Date date = null;
        try {
            date=formatter.parse(stringdate); //将Spring类型转成Date类型
        }catch (Exception e){
            e.printStackTrace();
        }

        /**
         * 实现文件上传
         */
        if(file.isEmpty()){
            return "false";
        }
        String fileName = file.getOriginalFilename();
        int size = (int) file.getSize();
        System.out.println(fileName + "-->" + size);

        String path = "F:/Java/StudyAndWork/IDEA/travel/src/main/resources/static/img/test";
        //F:\Java\StudyAndWork\IDEA\travel\src\main\resources\static\img\test\anewf.jpg
        File dest = new File(path + "/" + fileName);
        if(!dest.getParentFile().exists()){ //判断文件父目录是否存在
            dest.getParentFile().mkdir();
        }
        try {
            file.transferTo(dest); //保存文件
        } catch (IOException e) {
            e.printStackTrace();
            return "false";
        }
        //String imgUrl = path + "/" + fileName;
        String imgUrl = "../img/test/" + fileName;

        System.err.println("date : " + date);

        if(StringUtils.isEmpty(account)){
            return "用户名不能为空";
        }

        if(StringUtils.isEmpty(merchantname)){
            return "企业名称不能为空";
        }

        if(StringUtils.isEmpty(phone)){
            return "手机号码不能为空";
        }

        if(StringUtils.isEmpty(eamil)){
            return "邮箱不能为空";
        }

        if(StringUtils.isEmpty(password)){
            return "密码不能为空";
        }
        if(StringUtils.isEmpty(password2)){
            return "确认密码不能为空";
        }

        if(!password.equals(password2)){
            return "两次密码不相同，注册失败！！";
        }else {
            int res = impl.insertMerchant(account,merchantname,phone,eamil,imgUrl,date,statu,password);
            if(res == 0){
                //return "注册失败！";
                return "Login";
            }else {
                //return "注册成功！";
                return "shang/index";
            }
        }

    }


    /**
     * 获取用户名与密码，用户登录
     * @return 登录成功页面
     * @param account
     * @param password
     * @param request
     * @return
     */
    @RequestMapping(value = {"/dologin"})
    public String userLogin(@RequestParam("account") String account,
                            @RequestParam("account") String phone,
                            @RequestParam("account") String eamil,
                            @RequestParam("password") String password,
                            HttpServletRequest request){

        if(StringUtils.isEmpty(account)){
            return "用户名不能为空";
        }
        if(StringUtils.isEmpty(password)){
            return "密码不能为空";
        }

        System.err.println("account:"+account+"\nphone:"+phone+"\neamil:"+eamil+"\npassword:"+password);
        Merchant merchant = impl.businessLogin(account,phone,eamil,password);

        if(merchant != null){                                                  //登录成功
            request.getSession().setAttribute("merchant",merchant);     //将用户信息放入session  用于后续的拦截器
            //return "登录成功";
            return "shang/index";
        }
        //return "登录失败，用户名或密码错误";
        return "Login";
    }


    /**
     * 商家列表
     * @return
     *//*
    @RequestMapping("businessList")
    public String businessList(Merchant merchant,Model model){
        List<Merchant> list = impl.businessList();
        model.addAttribute("business",list);
        return "houtai/banner-list";
    }
*/

    /**
     * 根据企业名查询
     * @param merchantname
     * @return
     *//*
    @ResponseBody
    @RequestMapping("businessAndName")
    public Object businessAndName(@RequestParam("businessName") String merchantname){
        Merchant merchant = impl.businessAndName(merchantname);
        System.err.println(merchantname);
        return merchant;
    }*/


    /**
     * 分页
     * @param merchantname
     * @param pageIndex
     * @param model
     * @return
     */
    @RequestMapping("businessAndPage")
    public String businessAndPage(String pageIndex,String merchantname,Model model){
        Integer index = 1;// 默认页码为第一页

        // 页码
        if (pageIndex != null && !"".equals(pageIndex)&&!"null".equals(pageIndex)) {
            index = Integer.valueOf(pageIndex);
        }

        //分页 第一查询总条数
        Integer totalCount = impl.findCount();
        List<Merchant> merchant = impl.findPage(index,10,merchantname);
        model.addAttribute("merchantList", merchant);
        model.addAttribute("merchantname",merchantname);
        System.err.println("merchant:" + merchant);
        System.err.println(merchantname);

        PageSupport ps = new PageSupport();
        ps.setCurrentPageNo(index);
        ps.setPageSize(10);
        ps.setTotalCount(totalCount);

        Integer totalPageCount = ps.getTotalPageCount(); // 总页数

        /*model.addAttribute("totalPageCount", totalPageCount);
        model.addAttribute("page", index);*/

        model.addAttribute("page", ps);
        System.err.println(totalCount);
        System.err.println(totalPageCount);
        System.err.println(index);

        return "houtai/banner-list";
    }


    /**
     * 根据ID查询商家详情
     * @param @RequestParam(value="id") String businessId,
     * @return
     */
    @ResponseBody
    @RequestMapping("businessAndId")
    public Object businessAndId(Integer id){
        Merchant merchant = impl.businessAndId(id);
        System.err.println(merchant);
        return merchant;
    }


    /**
     * 状态 — 审批
     * @param status
     * @return
     */
    @ResponseBody
    @RequestMapping("businessAndStatus")
    public Object businessAndStatus(@RequestParam("status") Integer status,@RequestParam("id") Integer id){
       // System.err.println(status);
        impl.businessAndStatus(status,id);
       // System.err.println(id);
        return status;
    }








}

